package zhyioo.testframe;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

public class RecyclerViewAdapter extends
        RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    public interface OnItemClickLitener {
        void onItemClick(String str, int position);
    }

    private OnItemClickLitener mOnItemClickLitener = null;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private LayoutInflater mInflater;
    private List<String> mDatas;
    private Context mContext;

    public RecyclerViewAdapter(Context context, List<String> datats) {
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
        mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        TextView item_tv = null;
        Button item_btn = null;
        EditText item_et = null;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.recycler_item_layout, viewGroup,
                false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.item_tv = (TextView) view
                .findViewById(R.id.item_tv);
        viewHolder.item_btn = (Button) view
                .findViewById(R.id.item_btn);
        viewHolder.item_et = (EditText) view
                .findViewById(R.id.item_et);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        String data = mDatas.get(i);
        if (data.contains("+")) {
            viewHolder.item_tv.setVisibility(View.GONE);
            viewHolder.item_et.setVisibility(View.GONE);
            viewHolder.item_btn.setVisibility(View.VISIBLE);
            viewHolder.item_btn.setClickable(true);
            viewHolder.item_btn.setText(data.substring(1));
        } else if (data.contains("-")) {
            viewHolder.item_tv.setClickable(false);
            viewHolder.item_btn.setVisibility(View.GONE);
            viewHolder.item_tv.setVisibility(View.VISIBLE);
            viewHolder.item_et.setVisibility(View.GONE);
            viewHolder.item_tv.setText(data.substring(1));
        } else if (data.contains("=")) {
            viewHolder.item_tv.setVisibility(View.GONE);
            viewHolder.item_btn.setVisibility(View.GONE);
            viewHolder.item_et.setVisibility(View.VISIBLE);
            viewHolder.item_et.setHint(data.substring(1));
        } else {
            viewHolder.item_tv.setVisibility(View.GONE);
            viewHolder.item_et.setVisibility(View.GONE);
            viewHolder.item_btn.setVisibility(View.VISIBLE);
            viewHolder.item_btn.setClickable(true);
            viewHolder.item_btn.setText(data);
        }

        if (mOnItemClickLitener != null) {
            viewHolder.item_btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.item_btn.getText().toString(), i);
                }
            });
            viewHolder.item_et.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (viewHolder.item_et.getCompoundDrawables()[2] != null) {
                            boolean touchable = event.getX() > (viewHolder.item_et.getWidth() - viewHolder.item_et.getTotalPaddingRight())
                                    && (event.getX() < ((viewHolder.item_et.getWidth() - viewHolder.item_et.getPaddingRight())));
                            if (touchable) {
                                viewHolder.item_et.setFocusableInTouchMode(false);
                                viewHolder.item_et.setFocusable(false);
                                mOnItemClickLitener.onItemClick(viewHolder.item_et.getText().toString().equals("") ?
                                        viewHolder.item_et.getHint().toString() :
                                        viewHolder.item_et.getText().toString(), i);
                            } else {
                                viewHolder.item_et.setFocusableInTouchMode(true);
                                viewHolder.item_et.setFocusable(true);
                            }
                        }
                    }
                    return false;
                }
            });
        }
    }
}
